#pragma strict

////////////////////////////////////////////////////////////////////////////////////////////////
//VARS
////////////////////////////////////////////////////////////////////////////////////////////////

var sensitivity : int = 0.1;
var CameraKeySpeed : int = 300;
var CameraSpeed:int = 500;

private var mousedelta : Vector3;
private var TopScreenMove : Vector3;
private var BottomScreenMove : Vector3;
private var LeftScreenMove : Vector3;
private var RightScreenMove : Vector3;

private var isDrag : boolean = false;
private var displayCurses = 0;
private var displaySummons = 0;
private var MapWidth = 2000;
private var MapLenght = 2000;
private var ZombieSpawnTimeLimit :float = 1 ;
private var timePassedZombieSpawn :float = 0;

private var position : Vector3;
private var hit : RaycastHit;
private var ray;
var object : Transform;
var Object1 :GameObject;
var Object2 :GameObject;
var Object3 :GameObject;
var Object4 :GameObject;

////////////////////////////////////////////////////////////////////////////////////////////////
//START
////////////////////////////////////////////////////////////////////////////////////////////////


function Start() 
{
	TopScreenMove=Vector3(Screen.width,Screen.height-25,0);
	BottomScreenMove=Vector3(Screen.width,25,0);
	LeftScreenMove=Vector3(25,Screen.height,0);
	RightScreenMove=Vector3(Screen.width-25,Screen.height,0);
}

////////////////////////////////////////////////////////////////////////////////////////////////
//UPDATE
////////////////////////////////////////////////////////////////////////////////////////////////

function Update() 
{
	//MISC CAMERA FUNCTIONS --------------------------------------------------------------------------

	if(Input.GetKey("v"))
	{
		Camera.main.fieldOfView += 7*Time.deltaTime;
	}
	else if(Input.GetKey("b"))
	{
		Camera.main.fieldOfView += -7*Time.deltaTime;
	}
	if(Input.GetKeyDown("m"))
	{
		transform.position.y+=10;
	}
	else if(Input.GetKeyDown("n"))
	{
		transform.position.y += -10;
	}
	
	//MOUSE CAMERA CONTROL 1 --------------------------------------------------------------------------

 	if(Input.mousePosition.y>=TopScreenMove.y&&isDrag==false)
	{
		transform.position.z += CameraSpeed*Time.deltaTime;
			if(transform.position.z>MapWidth)
    	{
    	  transform.position.z = MapWidth;
    	  }

	}
	else if(Input.mousePosition.y<=BottomScreenMove.y&&isDrag==false)
	{
		transform.position.z += -CameraSpeed*Time.deltaTime;
		if(transform.position.z<0)
    	{
    	  transform.position.z = 0;
    	}
	}
	if(Input.mousePosition.x <=LeftScreenMove.x&&isDrag==false)
	{
		transform.position.x += -CameraSpeed*Time.deltaTime;
	    if(transform.position.x<0)
    	{
    	  transform.position.x = 0;
    	  }
	}
	else if(Input.mousePosition.x >=RightScreenMove.x&&isDrag==false)
	{
		transform.position.x += CameraSpeed*Time.deltaTime;
		if(transform.position.x>MapLenght)
    	{
    	  transform.position.x = MapLenght;
    	  }
	}

	//KEYBOARD CAMERA CONTROL -------------------------------------------------------------

    if(Input.GetKey("up"))
    {
    	transform.position.z +=CameraKeySpeed*Time.deltaTime;
    	if(transform.position.z>MapWidth)
    	{
    	  transform.position.z = MapWidth;
    	}
    }
     else if(Input.GetKey("down"))
    {
    	transform.position.z +=-CameraKeySpeed*Time.deltaTime;
    	if(transform.position.z<0)
    	{
    	  transform.position.z = 0;
    	}
    }
     if(Input.GetKey("left"))
    {
    	transform.position.x +=-CameraKeySpeed*Time.deltaTime;
    	if(transform.position.x<0)
    	{
    	  transform.position.x = 0;
    	  }
    }
     else if(Input.GetKey("right"))
    {
    	transform.position.x +=CameraKeySpeed*Time.deltaTime;
    	if(transform.position.x>MapLenght)
    	{
    	  transform.position.x = MapLenght;
    	  }
    }
    
   	//Mouse1, CLICK AND DRAG CAMERA CONTROL -----------------------------------------------
    if(Input.GetMouseButton(0))
    {	
        isDrag=true;
    	mousedelta = Vector3(-Input.GetAxis("Mouse X"),0,-Input.GetAxis("Mouse Y"));
		mousedelta *= sensitivity;
		transform.position.x +=sensitivity*mousedelta.x;
		transform.position.z +=sensitivity*mousedelta.z;
	}
	//Mouse0, SUMMONING --------------------------------------------------------------------------
	else if(Input.GetMouseButtonDown(1))
	{
		var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		if (Physics.Raycast (ray, hit)) 
		{
    		print(hit.point);
    		
    		if(timePassedZombieSpawn == 0)
    		{
    		timePassedZombieSpawn += Time.deltaTime;
    		print(timePassedZombieSpawn);
    		Instantiate (Object1,hit.point+Vector3(0, 1.4, 0), Quaternion.identity);
    		}
		}
		else {
    		print ("There is nothing beneath your mouse!");
  
		}
	}
	if (timePassedZombieSpawn > 0)
	{
		timePassedZombieSpawn +=Time.deltaTime;
		if ( timePassedZombieSpawn >= ZombieSpawnTimeLimit)
		{
		timePassedZombieSpawn=0;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////
//ONGUI
////////////////////////////////////////////////////////////////////////////////////////////////

function OnGUI (){
	var firstButton = GUI.Button(Rect(10,10,80,30), "CURSE");
	var secondButton = GUI.Button(Rect(10,50,80,30), "SUMMON");
	var thirdButton = GUI.Button(Rect (100,50,80,30),"BACON");
	
	if(thirdButton)
	{
		Object1 = Object2;
	}
		
	if(firstButton){
		if(displayCurses == 0){
			displayCurses = 1;
		}
		else{
			displayCurses = 0;
		}
	}
	if(secondButton){
		if(displaySummons == 0){
			displaySummons = 1;
		}
		else{
			displaySummons = 0;
		}
	}
	
	//SUMMONS MENU --------------------------------------------------------------------------
	if(displaySummons == 1){
		if(GUI.Button(Rect(10,90,80,30), "SUMMON1")){
		displaySummons = 0;
		}	
		if(GUI.Button(Rect(10,130,80,30), "SUMMON2")){
		displaySummons = 0;
		}	
		if(GUI.Button(Rect(10,170,80,30), "SUMMON3")){
		displaySummons = 0;
		}	
		if(GUI.Button(Rect(10,210,80,30), "SUMMON4")){
		displaySummons = 0;
		}	
	}
	//CURSES MENU --------------------------------------------------------------------------
	if(displayCurses == 1){
		if(GUI.Button(Rect(100,10,80,30), "CURSE1")){
		displayCurses = 0;
		}	
		if(GUI.Button(Rect(190,10,80,30), "CURSE2")){
		displayCurses = 0;
		}	
		if(GUI.Button(Rect(280,10,80,30), "CURSE3")){
		displayCurses = 0;
		}	
		if(GUI.Button(Rect(370,10,80,30), "CURSE4")){
		displayCurses = 0;
		}	
	}
	
	GUI.Label(Rect(Screen.width - 100,Screen.height - 30,100,50), "Active zombies:");
	
}